var Stp=null;
var layer={
  //提示层
  /*
    conent 提示内容
    that   传入当前页面this实例
    time   时间 传入单数即可 自动*1000
    icon   icon图标默认为普通
    fn     回调函数
  */
  msg: function (conent, that, time,icon='-', fn) {
    clearTimeout(Stp)
    that.setData({
      lay_type: 'layAlet',
      layer: {
        lay_alert_conent: conent,
        lay_if_show: true,
        lay_icon_index:icon==''?'-':icon
      }
    });
    Stp = setTimeout(function () {
      that.setData({
        layer: {
          lay_if_show: false,
        }
      });
      if (fn != 'function' && fn != undefined) {
        fn();
      }
    }, time * 1000);
  },
  //信息框
  /*
    conent 提示内容
    btn    按钮提示文字
    that   传入当前页面this实例
    yes    点击按钮回调
  */
  alert:function(conent,btn,that,yes){
    clearTimeout(Stp)
    that.setData({
      lay_type:'layMsg',
      layer:{
        lay_msg_conent: conent,
        lay_msg_btn: btn,
        lay_if_show: true,
      }
    });
    var pages = getCurrentPages();
    var curPage = pages[pages.length - 1];
    curPage.yes=function(){
      that.setData({
        layer:{
          lay_if_show: false,
        }
      });
      if (yes != 'function' && yes != undefined) {
        yes();
      }
    }
  },
  //确认框
  /*
    conent 提示内容
    btn    按钮提示[]数组
    that   传入当前页面this实例
    icon   图标
    yes    点击第2个按钮回调
    no     点击第1个按钮回调
  */
  confirm: function (conent, btn=['取消','确定'],that,icon='-',yes,no){
    clearTimeout(Stp)
    that.setData({
      lay_type: 'layInfo',
      layer:{
        lay_info_conent: conent,
        lay_info_btn: btn,
        lay_if_show: true,
        lay_icon_index: icon == '' ? '-' : icon
      }
    });
    var pages = getCurrentPages();
    var curPage = pages[pages.length - 1];
    curPage.yes = function () {
      that.setData({
        layer:{
          lay_if_show: false
        }
      });
      if (typeof yes=='function') {
        yes();
      }
    }
    curPage.no = function () {
      that.setData({
        layer:{
          lay_if_show: false
        }
      });
      if (no != 'function' && no != undefined) {
        no();
      }
    };
    curPage.close=function(){
      that.setData({
        layer: {
          lay_if_show: false
        }
      });
    }
  },
  //头部提示框
  /*
    conent 提示内容
    that   传入当前页面this实例
    time   时间
    bg     背景颜色
    icon   提示图标 支持 0 1 2
    fn     回调函数
  */
  top: function (conent, that, time, bg = '',icon='-',fn){
    clearTimeout(Stp)
    that.setData({
      lay_type: 'layTop',
      layer:{
        lay_top_conent: conent,
        lay_if_show: true,
        lay_top_bg:bg,
        lay_top_icon: icon
      }
    });
    Stp= setTimeout(function () {
      that.setData({
        layer:{
          lay_if_show: false
        }
      });
      if (fn != 'function' && fn != undefined) {
        fn();
      }
    }, time * 1000);
  },
  //自定义标题
  /*
    title  标题文字
    hue    标题背景色
    conent 提示内容
    that   当前页面this实例
    yes    回调函数
  */
  custom:function(title, hue, conent, that,yes){
    clearTimeout(Stp)
    that.setData({
      lay_type: 'layShade',
      layer:{
        lay_custom_hue: hue,
        lay_custom_title: title,
        lay_custom_conent: conent,
        lay_if_show: true
      }
    });
    var pages = getCurrentPages();
    var curPage = pages[pages.length - 1];
    curPage.yes = function () {
      that.setData({
        layer:{
          lay_if_show: false
        }
      });
      if (yes != 'function' && yes != undefined) {
        yes();
      }
    }
  },
  //状态提示层
  /*
    conent  提示内容
    is_type 状态值 默认success
    that    当前页面this实例
    time    时间
    fn      回调函数
  */
  toast:function(conent,is_type=0,that,time,fn){
    clearTimeout(Stp)
    if (is_type==0||is_type==''){
      that.setData({
        lay_type: 'layToast',
        layer: {
          lay_toast_conent: conent,
          lay_if_show: true,
          is_success:true
        }
      })
    } else if (is_type == 1){
      that.setData({
        lay_type: 'layToast',
        layer: {
          lay_toast_conent: conent,
          lay_if_show: true,
          is_success: false
        }
      })
    }else{
      this.alert('目前只支持success和error,请不要搞事情',that,4);
      return false;
    }
    Stp = setTimeout(function () {
      that.setData({
        layer: {
          lay_if_show: false,
        }
      });
      if (typeof fn == 'function') {
        fn();
      }
    }, time * 1000);
  },
  /*
    text  提示内容
    that    当前页面this实例
  */
  loading:function(text='',that){
    that.setData({
      lay_type:'layLoad',
      layer:{
        lay_if_show: true,
        layer_load_text:text
      }
    })
  },
  bottom: function (conent, btn, that, yes,other) {
    clearTimeout(Stp)
    that.setData({
      lay_type: 'layBottom',
      layer: {
        lay_msg_conent: conent,
        lay_msg_btn: btn,
        lay_if_show: true,
      }
    });
    var pages = getCurrentPages();
    var curPage = pages[pages.length - 1];
    console.info(curPage);
    curPage.yes = function () {
      that.setData({
        layer: {
          lay_if_show: false,
        }
      });
      if (yes != 'function' && yes != undefined) {
        yes();
      }
    }
    curPage.other = function () {
      that.setData({
        layer: {
          lay_if_show: false,
        }
      });
      if (other != 'function' && other != undefined) {
        other();
      }
    }
  },
  bottomShare: function (conent, btn, that, yes, other) {
    clearTimeout(Stp)
    that.setData({
      lay_type: 'layShare',
      layer: {
        lay_msg_conent: conent,
        lay_msg_btn: btn,
        lay_if_show: true,
      }
    });
    var pages = getCurrentPages();
    var curPage = pages[pages.length - 1];
    console.info(curPage);
    curPage.yes = function () {
      that.setData({
        layer: {
          lay_if_show: false,
        }
      });
      if (yes != 'function' && yes != undefined) {
        yes();
      }
    }
    curPage.other = function () {
      that.setData({
        layer: {
          lay_if_show: false,
        }
      });
      if (other != 'function' && other != undefined) {
        other();
      }
    }
  },
  //关闭所有弹出层
  close:function(that){
    that.setData({
      layer: {
        lay_if_show: false,
      }
    })
  },
  ini:function(that){
    clearTimeout(Stp)
    that.setData({
      lay_type:'layMsg'
    })
  }
}
module.exports = {
  layer: layer
}